﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace HappyTravelling.Abs
{
    public partial class TabControl : UserControl
    {
        private int index = 0;

        public int PivotSelectedIndex
        {
            get
            {
                return index;
            }
            set
            {
                index = value;
                SetTabs();
            }
        }

        private int tabs = 0;

        public int PivotTabs
        {
            get
            {
                return tabs;
            }
            set
            {
                tabs = value;
                SetTabs();
            }
        }

        private SolidColorBrush brush = Application.Current.Resources["MoneyWhiteBrush"] as SolidColorBrush;

        public SolidColorBrush ColorBrush
        {
            get
            {
                return brush;
            }
            set
            {
                brush = value;
                SetTabs();
            }
        }

        private int size = 10;

        public int Size
        {
            get
            {
                return size;
            }
            set
            {
                size = value;
                SetTabs();
            }
        }

        public event EventHandler IndexChanged;

        public TabControl()
        {
            InitializeComponent();
            PivotSelectedIndex = 0;
            PivotTabs = 0;
        }

        public void SetTabs()
        {
            try
            {
                if (tabs > 0)
                {
                    TabArea.Children.Clear();
                    for (int i = 0; i < tabs; i++)
                    {
                        TabArea.Children.Add(CreateNonSelectedEllipse(i));
                    }
                    if (index >= 0 && index < tabs)
                        TabArea.Children[index] = CreateSelectedEllipse(index);
                }
            }
            catch { }
        }

        public Ellipse CreateNonSelectedEllipse(int id)
        {
            var el = new Ellipse
            {
                Width = size,
                Height = size,
                Stroke = brush,
                StrokeThickness = 2,
                Margin = new Thickness(12),
                Fill = new SolidColorBrush(Colors.Transparent),
                Tag = id
            };
            //el.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(Tab_Tap);
            return el;
            //<Ellipse Width="8" Height="8" Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="2" Margin="12"/>
        }

        public Ellipse CreateSelectedEllipse(int id)
        {
            var el = new Ellipse
            {
                Width = size + 4,
                Height = size + 4,
                Fill = brush,
                Margin = new Thickness(8),
                Tag = id
            };
            //el.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(Tab_Tap);
            return el;
            //<Ellipse Width="12" Height="12" Fill="{StaticResource PhoneForegroundBrush}" Margin="8"/>
        }

        private void Tab_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                index = Convert.ToInt32(((Ellipse)sender).Tag);
                SetTabs();
                IndexChanged(this, null);
            }
            catch { }
        }
    }
}